package algorithm.encryption_decryption.aes;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * AES加密算法即密码学中的高级加密标准（Advanced Encryption Standard，AES）是美国联邦政府采用的一种区块加密标准。
 * 这个标准用来替代原先的DES
 * <p>
 * 支持AES 256位密钥的扩展包
 * 下载http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
 * 下载之后得到 local_policy.jar ，US_export_policy.jar 两个jar包，把这两个jar包放到 jdk/lib/security 目录下
 * 替换原来的两个jar包即可
 */
public class AES_ECB {

    /**
     * 算法
     */
    private static final String AES_ECB_PKS5PADDING = "AES/ECB/PKCS5Padding";


    public static void main(String[] args) throws Exception {
        //密钥 AES加密要求key必须要128个比特位（这里需要长度为16，否则会报错）
        String KEY = "88888888888999qq";
        String content = "url：findNames.action232131231231231dsdasd23131321231321231214234545564756758574323";
        System.out.println("加密前：" + content);

        System.out.println("加密密钥和解密密钥：" + KEY);

        String encrypt = aesEncrypt(content, KEY);
        System.out.println("加密后：" + encrypt);

        String decrypt = aesDecrypt(encrypt, KEY);

        System.out.println("解密后：" + decrypt);
    }

    /**
     * base 64 encode
     *
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    private static String base64Encode(byte[] bytes) {
        return new String(Base64.getEncoder().encode(bytes));
    }

    /**
     * base 64 decode
     *
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     */
    private static byte[] base64Decode(String base64Code) {
        return base64Code == null ? null : Base64.getDecoder().decode(base64Code);
    }


    /**
     * AES加密
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的byte[]
     */
    private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(AES_ECB_PKS5PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

        return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    }


    /**
     * AES加密为base 64 code
     *
     * @param content 待加密的内容
     * @return 加密后的base 64 code
     */
    private static String aesEncrypt(String content, String decryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, decryptKey));
    }

    /**
     * AES解密
     *
     * @param encryptBytes 待解密的byte[]
     * @param decryptKey   解密密钥
     * @return 解密后的String
     */
    private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);

        Cipher cipher = Cipher.getInstance(AES_ECB_PKS5PADDING);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));

        byte[] decryptBytes = cipher.doFinal(encryptBytes);

        return new String(decryptBytes);
    }


    /**
     * 将base 64 code AES解密
     *
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     */
    private static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return encryptStr == null ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }
}
